package com.ruoyi.framework.security;

import cn.dev33.satoken.stp.StpInterface;
import com.ruoyi.framework.security.service.SysPermissionService;
import com.ruoyi.web.system.domain.SysUser;
import com.ruoyi.web.system.service.ISysRoleService;
import com.ruoyi.web.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 自定义权限验证接口扩展
 * https://sa-token.cc/doc.html#/use/jur-auth
 */
@Component    // 打开此注解，保证此类被springboot扫描，即可完成sa-token的自定义权限验证扩展
public class SaTokenInterfaceImpl implements StpInterface {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        logger.debug("========SaTokenInterfaceImpl .getPermissionList 获取权限");
        // 本list仅做模拟，实际项目中要根据具体业务逻辑来查询权限
        SysUser user = userService.selectUserById(Long.parseLong(loginId.toString()));
        Set<String> permissionSet = permissionService.getMenuPermission(user);
        return new ArrayList<String>(permissionSet);
    }

    /**
     * 返回一个账号所拥有的角色标识集合
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 本list仅做模拟，实际项目中要根据具体业务逻辑来查询角色
        Set<String> roles = roleService.selectRolePermissionByUserId(Long.parseLong(loginId.toString()));
        return new ArrayList<String>(roles);
    }

}
